<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="试题图片地址" prop="questionPicture">-->
<!--        <el-input-->
<!--          v-model="queryParams.questionPicture"-->
<!--          placeholder="请输入试题图片地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="答案图片地址" prop="answerPicture">-->
<!--        <el-input-->
<!--          v-model="queryParams.answerPicture"-->
<!--          placeholder="请输入答案图片地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="老师的图片" prop="teacherPicture">-->
<!--        <el-input-->
<!--          v-model="queryParams.teacherPicture"-->
<!--          placeholder="请输入老师的图片"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="学生id" prop="studentId">-->
<!--        <el-input-->
<!--          v-model="queryParams.studentId"-->
<!--          placeholder="请输入学生id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="老师id" prop="teacherId">-->
<!--        <el-input-->
<!--          v-model="queryParams.teacherId"-->
<!--          placeholder="请输入老师id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否正确" prop="result">-->
<!--        <el-input-->
<!--          v-model="queryParams.result"-->
<!--          placeholder="请输入是否正确"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:paper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:paper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:paper:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:paper:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-collapse :accordion="true" @change="tableColl">
            <el-collapse-item :title="item.sysUser.nickName" :name="item.examinationAndUserId" v-for="(item,index) in props.row.examinationAndUserList" >
<!--              当前试卷的题目-->
              <el-table :data="item.timu" border>
                <el-table-column label="大题" align="center" prop="examinationPaperDetails.bigProblem" />
                <el-table-column label="小题" align="center" prop="examinationPaperDetails.questions" />
                <el-table-column label="状态" align="center" prop="examinationPaperDetails.questions" >
                  <template slot-scope="scope">
                    <el-tag v-if="scope.row.status == 0||scope.row.status == null"  type="danger">未批阅</el-tag>
                    <el-tag v-else type="success">已批阅</el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row,item,props.row)"
                      v-hasPermi="['system:paper:edit']"
                    >去批阅</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['system:paper:remove']"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
<!--              当前试卷的题目-->
            </el-collapse-item>
          </el-collapse>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="index" />
      <el-table-column label="考试名称" align="center" prop="examinationName" >
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="testStartTime" >

      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="testEndTime" >

      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0">未批阅</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">已批阅</el-tag>

        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {getPaper, delPaper, addPaper, updatePaper, listOnlinePaper, getOnlineStudentAnswer} from "@/api/markingPapers";
import {getExaminationDetail} from "@/api/examination";

export default {
  name: "Paper",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 作业试卷表格数据
      paperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        questionPicture: null,
        answerPicture: null,
        teacherRemarks: null,
        teacherPicture: null,
        studentId: null,
        teacherId: null,
        result: null,
        status: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        questionPicture: [
          { required: true, message: "试题图片地址不能为空", trigger: "blur" }
        ],
        answerPicture: [
          { required: true, message: "答案图片地址不能为空", trigger: "blur" }
        ],
        studentId: [
          { required: true, message: "学生id不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "老师id不能为空", trigger: "blur" }
        ],
        result: [
          { required: true, message: "是否正确不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询作业试卷列表 */
  async  getList() {
      this.loading = true;
      listOnlinePaper(this.queryParams).then(response => {
        response.rows.map((ele,index)=>{
          ele['index']=(this.queryParams.pageNum-1)*this.queryParams.pageSize+1
          // getExaminationDetail(ele.examinationId).then(res=>{
          //   ele['timu']=res.data.examinationPaperSummary.examinationPaperDetailsList
          // })
          ele.examinationAndUserList.forEach(inner=>{

            getOnlineStudentAnswer(inner.examinationAndUserId).then(res=>{
              inner['timu']=res.data.studentTestAnswerList
            })
          })
        })




        this.paperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jobTestPaperId: null,
        questionPicture: null,
        answerPicture: null,
        teacherRemarks: null,
        teacherPicture: null,
        studentId: null,
        teacherId: null,
        result: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jobTestPaperId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/markingPapers/offlineEntry')
    },
    /** 修改按钮操作 */
    handleUpdate(row,item,shijuan) {
      console.log(row,item,shijuan)
      localStorage.setItem('studentDetail',JSON.stringify(item.sysUser))
      this.$router.push({ path: '/markingPapers/markingPapers/components/editDetail', query: { id: row.studentTestAnswerId,type:'onLine'} })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jobTestPaperId != null) {
            updatePaper(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaper(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobTestPaperIds = row.jobTestPaperId || this.ids;
      this.$modal.confirm('是否确认删除作业试卷编号为"' + jobTestPaperIds + '"的数据项？').then(function() {
        return delPaper(jobTestPaperIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/paper/export', {
        ...this.queryParams
      }, `paper_${new Date().getTime()}.xlsx`)
    },
    //展开table子级
    tableColl(a,b){
      console.log(a,b)
    }
  }
};
</script>
